﻿using MediatR;
using Warehouse.API.Applcation.Command.EnterStoragesCommand;
using Warehouse.Infrstructre.Interface;

namespace Warehouse.API.Applcation.CommandHandlers.EnterStoragesCommandHandler
{
    /// <summary>
    /// 审核入库单状态
    /// </summary>
    public class EnterStoragesUpdateStateCommandHandler : IRequestHandler<EnterStoragesUpdateStateCommand, int>
    {
        protected readonly IEnterStoragesInfoRepsitory infoRep;
        protected readonly IEnterStoragesRepsitory entRep;

        public EnterStoragesUpdateStateCommandHandler(IEnterStoragesInfoRepsitory infoRep, IEnterStoragesRepsitory entRep)
        {
            this.infoRep = infoRep;
            this.entRep = entRep;
        }

        public async Task<int> Handle(EnterStoragesUpdateStateCommand request, CancellationToken cancellationToken)
        {
            var list = await entRep.GetModelAsync(request.id);
            var newlist = (await infoRep.GetAllAsyns()).Where(x => x.StockId == list.StockId);
            foreach (var item in newlist)
            {
                item.StateSP = request.state;
                item.ShenHePeople = request.People;
                item.ShenHeTime = DateTime.Now;
                await infoRep.UpdateAsync(item);
            }
            return 1;
        }


    }
}
